
export function createRowData(arrs, initIndex = 0) {
    const obj = {};
    arrs.forEach((v, index) => {
        obj[`col${index + initIndex}Title`] = v
    });
    return obj;
}


export function tableDataToApi() {
 }
 
let isToast = false;
export function entryListToTable(entryList, that) {
    const list = [];
    let nos = [];
    entryList.forEach((v, entryIndex) => {
        const { itemList = [], ...otherEntrys } = v;
        list.push(
          ...itemList.map((item, index) => {
            if (item.borrowAmount && !item.borrowSubject) {
                nos.push(v.no);
            }
            return {
              ...otherEntrys,
              ...item,
              rowSpan: index === 0 ? itemList.length : null,
              isHigh: entryIndex % 2 !== 0,
            };
          })
        );
      });
      
      if (nos.length && !isToast) {
        nos = [...new Set(nos)];
        isToast = true;
        that && that.$message({
            showClose: true,
            type: 'error',
            message: `分录${nos.join('、')}行科目为空，可能由税务相关的回单引起，请人工进行修改`,
            duration: 20000,
            onClose() {
                isToast = false;
            }
        })
      }
      return list;
}


export function balanceDataToTable(balance) {
    const {
        asset = {},
        liability = {},
        ownerEquity = {},
        totalLiabilityEquity,
    } = balance;
    const { currentAssets } = asset;
    const list = [];
    let endLength = 0;

    liability.ownerEquity = ownerEquity;

    ['currentAssets', 'noCurrentAssets', 'totalItem'].forEach((v) => {
        asset[v] && balanceItemFilter(v, asset[v], list, 'assets', false)
    });

    ['liability'].forEach(nameItem => {
        if (!balance[nameItem]) {
            return;
        }
        Object.keys(balance[nameItem]).forEach(v => {
            endLength = balanceItemFilter(v, balance[nameItem][v], list, nameItem, true, endLength).endListLength
        })
    })

    if (totalLiabilityEquity) {
        const { no, startValue, endValue, yearStartValue } = totalLiabilityEquity;
        rightAddList(list, endLength, createRowData([
            BALANCE_TITLES.totalLiabilityEquityTotal,
            no,
            endValue,
            yearStartValue,
            // curPeriodValue,
        ], 4))
    }
    return list;
}

function balanceItemFilter(v, itemData, list, name, isRight, endListlength = 0) {
    const currentAssetsTitles = getNullArray(5);
    currentAssetsTitles[0] = BALANCE_TITLES[v];
    const isTotal = v === 'totalItem'
    let nowLength = endListlength;

    if (v === 'ownerEquity') {
        nowLength += 6;
    }
    
    if (!isTotal) {
        const data = {
            ...createRowData(currentAssetsTitles, isRight ? 4 : 0),
        }
        if (isRight) {
            rightAddList(list, nowLength, data)
            // if (!list[nowLength].columnIndexs) {
            //     list[nowLength].columnIndexs = [...getNullArray(5, { isAdd: true })];
            //     list[nowLength].colSpans = [...getNullArray(5, { initValue: 1 })];
            // }
            // list[nowLength].columnIndexs.push(5);
            // list[nowLength].colSpans.push(5)

            nowLength++;
        }
        else {
            // Object.assign(data, {
            //     columnIndexs: [0],
            //     colSpans: [5]
            // })
            list.push(data);
        }
    }
    if (itemData && itemData.itemList) {
        itemData.itemList.forEach((item, itemIndex) => {
            const data = createRowData([item.key, item.no, item.endValue, item.yearStartValue], isRight ? 4 : 0)

            if (isRight) {
                rightAddList(list, nowLength + itemIndex, data);
            }
            else {
                list.push(data)
            }

        })

        nowLength += itemData.itemList.length;
    }
    if (isTotal || itemData.totalItem) {
        const item = isTotal ? itemData : itemData.totalItem;
        const data = createRowData(
            [BALANCE_TITLES[`${isTotal ? name : v}TotalItem`], item.no, item.endValue, item.yearStartValue], isRight ? 4 : 0
        );
        if (isRight) {
            rightAddList(list, nowLength, data);
            nowLength++;
        }
        else {
            list.push(data)
        }
    }

    return {
        endListLength: nowLength,
    };
}


export function getNullArray(length, {
    isAdd = false,
    initValue = ''
} = {}) {
    return Array.apply(null, { length }).map(
        (v, index) => isAdd ? index : initValue
    );
}

function rightAddList(list, index, data) {
    if (!list[index]) {
        list[index] = {};
    }
    Object.assign(list[index], data)
}

const BALANCE_TITLES = {
    assetsTotalItem: '资产总计',
    currentAssets: '流动资产',
    noCurrentAssets: '非流动资产',
    currentAssetsTotalItem: '流动资产合计',
    noCurrentAssetsTotalItem: '非流动资产合计',
    currentLiabilities: '流动负债',
    noCurrentLiabilities: '非流动负债',
    currentLiabilitiesTotalItem: '流动负债合计',
    noCurrentLiabilitiesTotalItem: '非流动负债合计',
    liabilityTotalItem: '负债合计',
    ownerEquity: '所有者权益（或股东权益）',
    ownerEquityTotalItem: '所有者权益（或股东权益）合计',
    totalLiabilityEquityTotal: '负债和所有者权益（或股东权益）总计'
}